package com.aaa.controller;

import com.aaa.Tool.Split_str;
import com.aaa.Tool.TXT_file_tools;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;

@Controller
@RequestMapping(value = "/admin")
public class Admin {

//    @Autowired
//    private SQL_Mapper sql_mapper;


    @PostMapping(value = "/add")
    @ResponseBody
    public boolean add(Model model, @RequestBody String json_data) {
        System.out.println(json_data);
        JsonParser parser = new JsonParser();  // 创建JsonParser对象
        JsonElement jsonElement = parser.parse(json_data); // 解析JSON字符串
        JsonObject jsonObject = jsonElement.getAsJsonObject(); // 将JsonElement转换为JsonObject

        String file_name = jsonObject.get("file_name").getAsString();
        if (!file_name.equals("Add_File")) {
            return false;
        }

        long timestamp = System.currentTimeMillis();  // 时间戳
        file_name = String.valueOf(timestamp) + ".txt";  // Convert timestamp to string

        String title = jsonObject.get("title").getAsString();
        String content = jsonObject.get("content").getAsString();

        Split_str split_str = new Split_str();
        String all_file_data = title + "\n\n" + split_str.strrrrrr + "\n\n" + content;

        TXT_file_tools txt_file_tools = new TXT_file_tools();
        boolean b = txt_file_tools.write_to_txt(file_name, all_file_data);
        return b;  // 是否写入成功
    }


    @PostMapping(value = "/update")
    @ResponseBody
    public boolean update(Model model, @RequestBody String json_data) {
        System.out.println(json_data);
        JsonParser parser = new JsonParser();  // 创建JsonParser对象
        JsonElement jsonElement = parser.parse(json_data); // 解析JSON字符串
        JsonObject jsonObject = jsonElement.getAsJsonObject(); // 将JsonElement转换为JsonObject

        String file_name = jsonObject.get("file_name").getAsString();
        String title = jsonObject.get("title").getAsString();
        String content = jsonObject.get("content").getAsString();

        Split_str split_str = new Split_str();
        String all_file_data = title + "\n\n" + split_str.strrrrrr + "\n\n" + content;

        TXT_file_tools txt_file_tools = new TXT_file_tools();
        boolean b = txt_file_tools.write_to_txt(file_name, all_file_data);
        return b;  // 是否写入成功
    }


    // 编辑模式
    @GetMapping(value = "/edit/{file_name}")
    @ResponseBody
    public String Get_File_Data(Model model, @PathVariable String file_name) {
        TXT_file_tools txt_file_tools = new TXT_file_tools();
        String txt_folder_path = txt_file_tools.get_txt_folder_path();
        String full_path = txt_folder_path + File.separator + file_name;
        String data = txt_file_tools.get_txt_file_data(full_path);
//        System.out.println(data);
        return data;
    }









}




